const TOBASE64 = true;
const strBase64 = (flag, str) => (flag === TOBASE64 ? Buffer.from(str).toString('base64') : Buffer.from(str, 'base64').toString());

export default {
  namespaced: true,
  state: {
    user: undefined,
    permissions: null,
    roles: null,
    routesConfig: null,
    enterpriseId: null,
    enterpriseList: null,
    userTab: null,
    userInfo: {},
    fromSystem: undefined,
    loginUserInfo: undefined,
    openId: undefined,
    token: undefined,
    roleId: undefined,
    appId: undefined,
    departmentId: undefined
  },
  getters: {
    permissions: (state) => {
      return state.permissions;
    },
    roles: (state) => {
      return state.roles;
    },
    routesConfig: (state) => {
      return state.routesConfig;
    },
    dictConfig: (state) => {
      if (!state.dictConfig) {
          try {
              const stateCopy = state;
              const dictConfig = strBase64(false, localStorage.getItem(process.env.VUE_APP_DICTS_KEY));
              stateCopy.dictConfig = JSON.parse(dictConfig);
              stateCopy.dictConfig = state.dictConfig ? state.dictConfig : [];
          } catch (e) {
              console.error(e.message);
          }
      }
      return state.dictConfig;
    },
    areaData: (state) => {
      if (!state.areaData) {
          try {
              const stateCopy = state;
              const areaData = strBase64(false, localStorage.getItem(process.env.VUE_APP_AREA_KEY));
              stateCopy.areaData = JSON.parse(areaData);
              stateCopy.areaData = state.areaData ? state.areaData : [];
          } catch (e) {
              console.error(e.message);
          }
      }
      return state.areaData;
    },
    enterpriseId: (state) => {
      return state.enterpriseId;
    },
    enterpriseList: (state) => {
      return state.enterpriseList;
    },
    userTab: (state) => {
      return state.userTab;
    },
    userInfo: state => {
      return state.userInfo
    },
    fromSystem: state => {
      return state.fromSystem
    },
    loginUserInfo: state => {
      return state.loginUserInfo
    },
    openId: state => {
      return state.openId
    },
    token: state => {
      return state.token
    },
    roleId: state => {
      return state.roleId
    },
    appId: state => {
      return state.appId
    },
    departmentId: state => {
      return state.departmentId
    }
  },
  mutations: {
    setPermissions(state, permissions) {
      state.permissions = permissions;
    },
    setRoles(state, roles) {
      state.roles = roles;
    },
    setRoutesConfig(state, routesConfig) {
      state.routesConfig = routesConfig;
    },
    setEnterpriseId(state, enterpriseId) {
      state.enterpriseId = enterpriseId;
    },
    setEnterpriseList(state, enterpriseList) {
      state.enterpriseList = enterpriseList;
    },
    setUserTab(state, userTab) {
      state.userTab = userTab;
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // 统一更新state的公共方法
    setNewState(state, newState) {
      Object.assign(state, newState)
    },
    setLoginUserInfo(state, loginUserInfo) {
      state.loginUserInfo = loginUserInfo
    }
  }
}
